#include "selectTool/selectToolPublic.h"

bool PointSelectCommand::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
	osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
	if (!view) return false;


	switch(ea.getEventType())
	{
	case(osgGA::GUIEventAdapter::DRAG):
		{
			break;
		}
	case(osgGA::GUIEventAdapter::PUSH): 
		{
			CommandBegin(ea, aa);
			break;
		}
	case(osgGA::GUIEventAdapter::RELEASE):
		{
			break;
		}
	default:
		break;
	}
	return false;
}

PointSelectCommand::PointSelectCommand(Interactiv_Base* bim_Interactiv, int type) : Command(bim_Interactiv, type)
{
	state_ = 0;

	bim_Interactiv_ = (InteractivBIM*)bim_Interactiv;

	int w = bim_Interactiv_->GetViewer()->getCamera()->getViewport()->width() ;
	int h = bim_Interactiv_->GetViewer()->getCamera()->getViewport()->height();

	
	bim_Interactiv_->getRootGroup()->addChild(projection_);
}

PointSelectCommand::~PointSelectCommand()
{

}

osg::Vec3d PointSelectCommand::WindowToWorldPoint(const osgGA::GUIEventAdapter& event, float p)
{
	osg::Vec3d startVertex;

	if (event.getNumPointerData()>=1)
	{
		const osgGA::PointerData* pd = event.getPointerData(event.getNumPointerData()-1);
		const osg::Camera* camera = dynamic_cast<const osg::Camera*>(pd->object.get());

		osg::Matrixd matrix;

		matrix.postMult(camera->getViewMatrix());
		matrix.postMult(camera->getProjectionMatrix());

		osg::Matrixd inverse;
		inverse.invert(matrix);

		startVertex = osg::Vec3d(pd->getXnormalized(),pd->getYnormalized(),p) * inverse;
	}

	return startVertex;
}


void PointSelectCommand::CommandBegin(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)  
{
	osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);

	if(ea.getButton() ==  osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
	{
		const osgGA::PointerData* pd = ea.getPointerData(ea.getNumPointerData()-1);
		const osg::Camera* camera = dynamic_cast<const osg::Camera*>(pd->object.get());

		if (!camera)
		{
			return;
		}

		
		osgUtil::LineSegmentIntersector::Intersections intersections;

		osg::Vec3d f = WindowToWorldPoint(ea, -1);
		osg::Vec3d s = WindowToWorldPoint(ea, 1) ;

		osg::ref_ptr< osgUtil::LineSegmentIntersector > picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::MODEL, f, s);
		osgUtil::IntersectionVisitor iv(picker.get());
		iv.setTraversalMask(0xffffffff);

		bim_Interactiv_->getBimRoot()->accept(iv);

		
		if (picker->containsIntersections())
		{
			intersections = picker->getIntersections();

			osg::Geode* geode = (osg::Geode*)(*intersections.begin()).drawable->getParent(0);

			osg::ref_ptr<osg::Material> material = new osg::Material;
			osg::Vec4 redColor(1.0, 0.0, 0.0, 1.0);
			material->setAmbient(osg::Material::FRONT_AND_BACK, redColor);
			material->setDiffuse(osg::Material::FRONT_AND_BACK,redColor);
			material->setColorMode(osg::Material::AMBIENT);
			geode->getOrCreateStateSet()->setAttributeAndModes(material.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

			//OcGrObjectId id = geode->getObjectId();

			int nn = 0.0;
		}
	}
}

void PointSelectCommand::CommandEnd(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{

}

void PointSelectCommand::CommandExe(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)  
{

}

